package com.xingxue.logistics.controller.sys;

import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.vo.Page;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.common.dto.SysUser;
import com.xingxue.logistics.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Kris at 2018/10/8 15:07
 */
@RestController
@RequestMapping("sys/user")
@Api(description = "用户相关操作")
public class UserController {

    @Autowired
    UserService userService;

    /*@ApiImplicitParams({
            //paramType通常为
            // query (url参数)  http://localhost:8080/api?name=xxx
            // path (路径参数)  http://localhost:8080/api/user/search/xxx ; @GetMapping("search/{id}")
            // body (http请求体)   http的请求体内部的完整内容通常用json
            // header (http请求头)   http请求头部，request.getHeader("header-name")
    })*/


    @ApiOperation("分页查询用户(EasyUI用)")
    @GetMapping("search_page_easyui")
    public ResponseEntity<Page<SysUser>> searchEasyUI(
            @RequestParam(required = false) @ApiParam("查询条件：用户ID") Integer id,
            @RequestParam(required = false) @ApiParam("查询条件：昵称") String nick,
            @RequestParam(required = false) @ApiParam("查询条件：账号") String account,
            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "当前页码", defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页行数", defaultValue = "10") Integer rows
    ) {
        PageInfo<SysUser> pageInfo = userService.searchPage(id, nick, account, page, rows);
        return ResponseEntity.ok(new Page<>(pageInfo));
    }

    @GetMapping("search")
    public ResponseEntity<List<SysUser>> loadUserByRoleId(@RequestParam(required = false) @ApiParam("查询条件：用户ID") Integer id,
                                                          @RequestParam(required = false) @ApiParam("查询条件：昵称") String nick,
                                                          @RequestParam(required = false) @ApiParam("查询条件：账号") String account,
                                                          @RequestParam(required = false) @ApiParam("查询条件：所属角色") Integer roleId,
                                                          @RequestParam(required = false) @ApiParam("查询条件：没有和某个角色关联的用户") Integer notJoinRoleId
    ) {
        List<SysUser> list = userService.search(id,nick,account,roleId,notJoinRoleId);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("创建一个用户")
    @PostMapping
    public ResponseEntity<RestResult> add(@RequestBody @ApiParam("用户") SysUser user) {
        int i = userService.addUser(user);
        if(i>0){
            return ResponseEntity.ok(new RestResult(true,"新增成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"新增失败",null));
    }

    @ApiOperation(value = "修改用户",notes = "根据主键修改")
    @PutMapping
    public ResponseEntity<RestResult> update(@RequestBody @ApiParam("用户") SysUser user) {
        int i = userService.updateUser(user);
        if(i>0){
            return ResponseEntity.ok(new RestResult(true,"修改成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"修改失败",null));
    }

    @ApiOperation(value = "删除用户",notes = "根据主键删除")
    @DeleteMapping("/{id}")
    public ResponseEntity<RestResult> delete(@PathVariable("id") @ApiParam("用户ID") Integer id) {
        int i = userService.deleteUser(id);
        if(i>0){
            return ResponseEntity.ok(new RestResult(true,"删除成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"删除失败",null));
    }

}
